package com.sk.cardeal.adapter;

import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sk.cardeal.R;
import com.sk.cardeal.callback.CountCallback;
import com.sk.cardeal.callback.ExpendListViewItemClickListener;
import com.sk.cardeal.callback.onGroupExpandedListener;
import com.sk.cardeal.entity.ByKindList;
import com.sk.cardeal.entity.ByPartsEntity;
import com.sk.cardeal.entity.KindList;
import com.sk.cardeal.entity.NewPartsList;

import java.util.List;

/**
 * Created by sk on 2018/4/13.
 */

public class ByExpendAdapter extends BaseExpandableListAdapter {

    private static final String TAG = "IndicatorExpandableList";
    private List<ByKindList.ResultListBean> groupData;
    private List<ByKindList.ResultListBean> childData;
    private ExpendListViewItemClickListener expendListViewItemClickListener;
    private CountCallback callback;

    public CountCallback getCallback() {
        return callback;
    }

    public void setCallback(CountCallback callback) {
        this.callback = callback;
    }

    public ByExpendAdapter(List<ByKindList.ResultListBean> groupData, List<ByKindList.ResultListBean> childData) {
        this.groupData = groupData;
        this.childData = childData;
    }

    public ExpendListViewItemClickListener getExpendListViewItemClickListener() {
        return expendListViewItemClickListener;
    }

    public void setExpendListViewItemClickListener(ExpendListViewItemClickListener expendListViewItemClickListener) {
        this.expendListViewItemClickListener = expendListViewItemClickListener;
    }

    @Override
    public int getGroupCount() {
        return groupData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return childData.get(groupPosition).getCarPartsList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childData.get(groupPosition).getCarPartsList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ByExpendAdapter.GroupViewHolder groupViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_by, parent, false);
            groupViewHolder = new ByExpendAdapter.GroupViewHolder();
            groupViewHolder.tv_name = convertView.findViewById(R.id.tv_name);
            groupViewHolder.icon = convertView.findViewById(R.id.icon);
            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (ByExpendAdapter.GroupViewHolder) convertView.getTag();
        }

        if(groupData.get(groupPosition).isChoose()){
            groupViewHolder.icon.setChecked(true);
        }else {
            groupViewHolder.icon.setChecked(false);
        }

        groupViewHolder.tv_name.setText(groupData.get(groupPosition).getPartsName());
        groupViewHolder.icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(expendListViewItemClickListener!=null){
                    expendListViewItemClickListener.expendItemClickListener(v,groupPosition,0);
                }
            }
        });

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View
            convertView, ViewGroup parent) {
        ByExpendAdapter.ChildViewHolder childViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_new_by_parts, parent, false);
            childViewHolder = new ByExpendAdapter.ChildViewHolder();
            childViewHolder.parts_name = convertView.findViewById(R.id.parts_name);
            childViewHolder.tv_price = convertView.findViewById(R.id.tv_price);
            childViewHolder.tv_num = convertView.findViewById(R.id.tv_num);
            childViewHolder.imageView = convertView.findViewById(R.id.image);
            childViewHolder.edit_btn = convertView.findViewById(R.id.edit_btn);
            childViewHolder.normal_ll = convertView.findViewById(R.id.normal_ll);
            childViewHolder.change_ll = convertView.findViewById(R.id.change_ll);

            childViewHolder.reduce = convertView.findViewById(R.id.reduce);
            childViewHolder.add = convertView.findViewById(R.id.add);
            childViewHolder.select_num = convertView.findViewById(R.id.select_num);
            childViewHolder.save = convertView.findViewById(R.id.save);
            childViewHolder.change = convertView.findViewById(R.id.change);

            convertView.setTag(childViewHolder);
        } else {
            childViewHolder = (ByExpendAdapter.ChildViewHolder) convertView.getTag();
        }
        ByPartsEntity entity = groupData.get(groupPosition).getCarPartsList().get(childPosition);


        childViewHolder.select_num.setText(String.valueOf(entity.getSelectCount()));
        childViewHolder.tv_num.setText("×"+entity.getSelectCount());
        childViewHolder.parts_name.setText(entity.getPartsName());
        childViewHolder.tv_price.setText("￥"+entity.getPrice());

        if(groupData.get(groupPosition).getCarPartsList().get(childPosition).isEdit()){
            childViewHolder.normal_ll.setVisibility(View.GONE);
            childViewHolder.change_ll.setVisibility(View.VISIBLE);
        }else {
            childViewHolder.normal_ll.setVisibility(View.VISIBLE);
            childViewHolder.change_ll.setVisibility(View.GONE);
        }


        childViewHolder.edit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                groupData.get(groupPosition).getCarPartsList().get(childPosition).setEdit(true);
                notifyDataSetChanged();
            }
        });

        childViewHolder.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                groupData.get(groupPosition).getCarPartsList().get(childPosition).setEdit(false);
                notifyDataSetChanged();
            }
        });

        childViewHolder.change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(expendListViewItemClickListener!=null){
                    expendListViewItemClickListener.expendItemClickListener(v,groupPosition,childPosition);
                }
            }
        });

        childViewHolder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(expendListViewItemClickListener!=null){
                    expendListViewItemClickListener.expendItemClickListener(v,groupPosition,childPosition);
                }
            }
        });

        childViewHolder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(expendListViewItemClickListener!=null){
                    expendListViewItemClickListener.expendItemClickListener(v,groupPosition,childPosition);
                }
            }
        });

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class GroupViewHolder {
        TextView tv_name;
        CheckBox icon;
    }

    private static class ChildViewHolder {
        TextView parts_name, tv_price, tv_num;
        ImageView imageView, edit_btn;
        RelativeLayout normal_ll, change_ll;
        TextView reduce, add, select_num;
        TextView save, change;
    }
}
